/**
 * 生成路由
 * @param {Array} routerlist 格式化路由
 * @returns
 */
export function addRouter(routerlist) {
  const router = []
  try {
    routerlist.forEach(item => {
      /**
       * 如果没有子路由
       *     children里添加当前元素
       * 如果有子路由
       *     children里添加当前元素子元素
       */
      let e_new = {
        path: '',
        name: '',
        component: resolve => require([`@/layout`], resolve)
      }
      if (item.children===undefined) {
        let e_new2 = []
        e_new2[0] = {
          path: item.linkUrl,
          name: item.linkUrl,
          meta: {
            title: item.label
          },
          component: resolve => require([`@/views/${item.component}`], resolve)
        }
        e_new = { ...e_new, children: e_new2 } // 保存权限
      }else if (item.children) {
        const children = addRouter2(item.children)
        e_new = { ...e_new, children: children } // 保存权限
      }
      router.push(e_new)
    })
  } catch (error) {
    console.error(error)
    return []
  }
  return router
}

function addRouter2(routerlist) {
  const router = []
  try {
    routerlist.forEach(e => {
      let e_new = {
        path: e.linkUrl,
        name: e.linkUrl,
        meta: {
          title: e.label
        },
        component: resolve => require([`@/views/${e.component}`], resolve)
      }
      router.push(e_new)
    })
  } catch (error) {
    console.error(error)
    return []
  }
  return router
}
